package com.ruoyi.food.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.environment.domain.OpmEnvRule;
import com.ruoyi.food.domain.OpmFoodSafeRule;
import com.ruoyi.food.mapper.OpmFoodSafeRuleMapper;
import com.ruoyi.food.service.IOpmFoodSafeRuleService;
import com.ruoyi.system.api.LinkFileServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 安全法规Service业务层处理
 *
 * @author cjie
 * @date 2021-07-24
 */
@Service
public class OpmFoodSafeRuleServiceImpl extends LinkFileServiceImpl implements IOpmFoodSafeRuleService {
    @Autowired
    private OpmFoodSafeRuleMapper opmFoodSafeRuleMapper;

    /**
     * 查询安全法规
     *
     * @param id 安全法规ID
     * @return 安全法规
     */
    @Override
    public OpmFoodSafeRule selectOpmFoodSafeRuleById(Long id) {
        OpmFoodSafeRule result = opmFoodSafeRuleMapper.selectOpmFoodSafeRuleById(id);
        result.setFileList(getFileList(id.toString()));
        return result;
    }

    /**
     * 查询安全法规列表
     *
     * @param opmFoodSafeRule 安全法规
     * @return 安全法规
     */
    @Override
    public List<OpmFoodSafeRule> selectOpmFoodSafeRuleList(OpmFoodSafeRule opmFoodSafeRule) {
        return opmFoodSafeRuleMapper.selectOpmFoodSafeRuleList(opmFoodSafeRule);
    }

    /**
     * 新增安全法规
     *
     * @param opmFoodSafeRule 安全法规
     * @return 结果
     */
    @Override
    public int insertOpmFoodSafeRule(OpmFoodSafeRule opmFoodSafeRule) {
        opmFoodSafeRule.setId(IdUtil.getSnowflake(0, 0).nextId());
        opmFoodSafeRule.setCreateTime(DateUtils.getNowDate());
        opmFoodSafeRule.setCreateBy(SecurityUtils.getUsername());
        linkFile(opmFoodSafeRule.getFileIdList(), opmFoodSafeRule.getId().toString());
        return opmFoodSafeRuleMapper.insertOpmFoodSafeRule(opmFoodSafeRule);
    }

    /**
     * 修改安全法规
     *
     * @param opmFoodSafeRule 安全法规
     * @return 结果
     */
    @Override
    public int updateOpmFoodSafeRule(OpmFoodSafeRule opmFoodSafeRule) {
        opmFoodSafeRule.setUpdateTime(DateUtils.getNowDate());
        opmFoodSafeRule.setUpdateBy(SecurityUtils.getUsername());
        linkFile(opmFoodSafeRule.getFileIdList(), opmFoodSafeRule.getId().toString());
        return opmFoodSafeRuleMapper.updateOpmFoodSafeRule(opmFoodSafeRule);
    }

    /**
     * 批量删除安全法规
     *
     * @param ids 需要删除的安全法规ID
     * @return 结果
     */
    @Override
    public int deleteOpmFoodSafeRuleByIds(Long[] ids) {
        String[] res = new String[ids.length];
        for(int i=0;i<ids.length;i++){
            res[i] = String.valueOf(ids[i]);
        }
        deleteLinkFile(res);
        return opmFoodSafeRuleMapper.deleteOpmFoodSafeRuleByIds(ids);
    }

    /**
     * 删除安全法规信息
     *
     * @param id 安全法规ID
     * @return 结果
     */
    @Override
    public int deleteOpmFoodSafeRuleById(Long id) {
        return opmFoodSafeRuleMapper.deleteOpmFoodSafeRuleById(id);
    }
}
